package com.leejie.springboot01;

public class A {
    public static void main(String[] args) {
        int[] a = {32,34,23,12,3,8,4,46,86,3};
        int[] ints = sort(a);
        for (int x :
                ints) {
            System.out.print(" "+x);
        }
    }

    //插入排序
    static int[] sort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            for (int j=i;j>0;j--){
                if (arr[j]<arr[j-1]){
                    int temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }


    //冒泡排序
    public static int[] sortArray(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length-i; j++) {
                if (arr[j-1]<arr[j]){
                    int swap = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = swap;
                }
            }
        }
        return arr;
    }

    //选择排序
    public static int[] sortArray2(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i+1; j < arr.length ; j++) {
                if (arr[j]>arr[i]){
                    int swap = arr[j];
                    arr[j] = arr[i];
                    arr[i] = swap;
                }
            }
        }
        return arr;
    }

    //插入排序
    public static int[] sortArray3(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i]; //记录待插入的元素
            int insertPos = -1; //初始化待插入点位置
            for (int j = i-1; j >= 0 ; j--) {
                if (arr[j]>temp){
                    arr[j+1] = arr[j];
                    insertPos = j; //更换插入点
                }else {
                    break;
                }
            }
            if (insertPos != -1){
                arr[insertPos] = temp;
            }
        }
        return arr;
    }
}
